package com.snj.visangedu;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;

import com.snj.visangedu.drm.VideoAct;
import com.snj.visangedu.lib.Global;
import com.snj.visangedu.lib.StudyviewFragment;

public class EdumaxFragment extends Fragment implements OnClickListener {
	private final String TAG = "EdumaxFragment";
	
	boolean isblue = false;
	boolean isrunning = true;
	
	ImageView img_postscript = null;
	ImageView img_start = null;
	
	ScrollView scrv_edumax = null;
	
	LinearLayout lily_edumax = null;
	LinearLayout lily_edumax_howto = null;
	

		
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View root = inflater.inflate(R.layout.fragment_edumax,
				container, false);
		return root;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d(TAG, "onActivityCreated");
		
		super.onActivityCreated(savedInstanceState);
		init_widget();
		
		new AsyncTask<String, String, String>() {

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				
				Log.d(TAG, "currentfragment : " + Global.CurrentFragment);
				while(Global.CurrentFragment == StudyviewFragment.EdumaxFragment)
				{
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				return null;
			}

			@Override
			protected void onProgressUpdate(String... values) {
				// TODO Auto-generated method stub
				super.onProgressUpdate(values);
				
				if(isblue)
				{
					img_start.setImageResource(R.drawable.new_edumax_16_1);
				}
				else
				{
					img_start.setImageResource(R.drawable.new_edumax_16);
				}
				
				isblue = !isblue;
				
			}
			
			
		}.execute("");
	}
	
	private void init_widget() {
		View view = getView();
		
		LinearLayout.LayoutParams param = null;
		
		lily_edumax = (LinearLayout)view.findViewById(R.id.lily_edumax);
		
		DisplayMetrics metrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int screenWidth = metrics.widthPixels;
		
		
		{
			for(int i=0; i<17; i++)
			{
				ImageView edumax1 = new ImageView(getActivity());
				param = new LayoutParams(screenWidth, LayoutParams.WRAP_CONTENT);		
				
				
				BitmapFactory.Options options = new BitmapFactory.Options();
				
				options.inJustDecodeBounds = true;
				//BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("new_edumax_" + i, "drawable", getActivity().getPackageName()), options);					
				options.inJustDecodeBounds = false;
				options.inPurgeable = true;
				options.inDither = true;
				options.inPreferredConfig = Config.RGB_565;
				edumax1.setAdjustViewBounds(true);
				
				Bitmap img = null;
				
				try
				{
					img = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("new_edumax_" + i, "drawable", getActivity().getPackageName()), options);
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				
				
				edumax1.setImageBitmap(img);
				
				switch(i)
				{
				case 0:
					edumax1.setBackgroundResource(R.color.edumax_color);
					edumax1.setPadding(100, 120, 100, 140);
					options.inSampleSize = 1;
					
					break;
				case 1:
					edumax1.setBackgroundResource(R.color.white_color);
					edumax1.setPadding(100, 80, 100, 0);
					options.inSampleSize = 1;
					break;
				case 3:
				case 8:
				case 10:
					ImageView edumax2 = new ImageView(getActivity());		
					edumax2.setPadding(0, 0, 100, 0);
					
					param = new LayoutParams(LayoutParams.MATCH_PARENT, 170);
					options.inSampleSize = 1;
					edumax2.setAdjustViewBounds(true);
					edumax2.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.edumax_line, options));
					edumax2.setBackgroundResource(R.color.white_color);
					edumax2.setLayoutParams(param);
					lily_edumax.addView(edumax2);
					edumax2.setScaleType(ScaleType.FIT_START);
					
					edumax1.setBackgroundResource(R.color.white_color);
					edumax1.setPadding(100, 20, 100, 0);
					options.inSampleSize = 1;
					break;
				case 4:
				case 9:
				case 11:
				case 13:
				case 14:
					edumax1.setBackgroundResource(R.color.white_color);
					
					if(i == 14)
					{
						edumax1.setPadding(0, 0, 0, 0);
					}
					else
					{
						edumax1.setPadding(0, 40, 0, 0);
					}
					options.inSampleSize = 2;
					//img.recycle();
					img = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("new_edumax_" + i, "drawable", getActivity().getPackageName()), options);
					
					edumax1.setImageBitmap(img);
					break;
					
				case 16:
					img_postscript = edumax1;
					img_postscript.setOnClickListener(this);
					//img.recycle();
					img = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("new_edumax_" + 17, "drawable", getActivity().getPackageName()), options);				
					edumax1.setImageBitmap(img);
					
					edumax1.setBackgroundResource(R.color.white_color);
					edumax1.setPadding(0, 40, 0, 0);
					options.inSampleSize = 1;
					break;
				default:
					edumax1.setBackgroundResource(R.color.white_color);
					edumax1.setPadding(100, 40, 100, 0);
					options.inSampleSize = 1;
					break;
				}
				
				
				edumax1.setScaleType(ScaleType.FIT_CENTER);
				lily_edumax.addView(edumax1);
				
				
				//img.recycle();
				
			}
		
			lily_edumax_howto = new LinearLayout(getActivity());
			lily_edumax_howto.setOrientation(LinearLayout.VERTICAL);
			param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			lily_edumax_howto.setLayoutParams(param);
			lily_edumax_howto.setBackgroundResource(R.drawable.edumax_howto_bg);
			
			for(int i=0; i<4; i++)
			{
				ImageView edumax1 = new ImageView(getActivity());
			
				BitmapFactory.Options options = new BitmapFactory.Options();
				
				//options.inJustDecodeBounds = true;
				//BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("new_edumax_" + i, "drawable", getActivity().getPackageName()), options);
				options.inSampleSize = 1;
				//options.inJustDecodeBounds = false;
	
				edumax1.setAdjustViewBounds(true);
				
				Bitmap img = null;
	
				try
				{
					img = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("new_edumax_howto_" + i, "drawable", getActivity().getPackageName()), options);
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				edumax1.setImageBitmap(img);
				
				edumax1.setScaleType(ScaleType.FIT_CENTER);
				
				switch(i)
				{
				case 0:
				case 1:
				case 2:
					edumax1.setPadding(0, 40, 0, 0);
					break;
				}
				lily_edumax_howto.addView(edumax1);
			}
			
			lily_edumax.addView(lily_edumax_howto);
			
			
			ImageView edumax1 = new ImageView(getActivity());		
			edumax1.setPadding(0, 0, 0, 0);
	
			img_start = edumax1;
			edumax1.setAdjustViewBounds(true);
			edumax1.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.new_edumax_16));
			edumax1.setBackgroundResource(R.color.white_color);
			lily_edumax.addView(edumax1);
			edumax1.setScaleType(ScaleType.FIT_CENTER);
			
			img_start.setOnClickListener(this);
			
			scrv_edumax = (ScrollView)view.findViewById(R.id.scrv_edumax);
			//scrv_edumax.getscr
			
			isrunning = true;
			new AsyncTask<String, String, String>() {
				
				
	
				@Override
				protected String doInBackground(String... params) {
					// TODO Auto-generated method stub
					
					while(isrunning)
					{
						try {
							Thread.sleep(500);
							publishProgress("0");
							Thread.sleep(500);
							publishProgress("1");
	
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					
					return null;
				}
	
				@Override
				protected void onProgressUpdate(String... values) {
					// TODO Auto-generated method stub
					super.onProgressUpdate(values);
					
					try
					{
						if(values[0].equals("0"))
						{
							img_postscript.setImageResource(R.drawable.new_edumax_17_1);
							img_start.setImageResource(R.drawable.new_edumax_16_1);
						}
						else
						{
							img_postscript.setImageResource(R.drawable.new_edumax_17);
							img_start.setImageResource(R.drawable.new_edumax_16);
						}
					}
					catch(Exception e)
					{
						e.printStackTrace();
					}
				}
	
				@Override
				protected void onPostExecute(String result) {
					// TODO Auto-generated method stub
					super.onPostExecute(result);
					
					Log.d(TAG, "PostExecute");
				}
				
				
				
			}.execute("");
		}
	}
	
	
	
	@Override
	public void onResume() {
		
		blank();
		Log.d(TAG, "onResume");
		
		super.onResume();
		
		
	}
	
	private void blank()
	{
		isrunning = true;
		new AsyncTask<String, String, String>() {

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				
				while(isrunning)
				{
					try {
						Thread.sleep(500);
						publishProgress("0");
						Thread.sleep(500);
						publishProgress("1");

					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				return null;
			}

			@Override
			protected void onProgressUpdate(String... values) {
				// TODO Auto-generated method stub
				super.onProgressUpdate(values);
				
				try
				{
					if(values[0].equals("0"))
					{
						img_postscript.setImageResource(R.drawable.new_edumax_17_1);
						img_start.setImageResource(R.drawable.new_edumax_16_1);
					}
					else
					{
						img_postscript.setImageResource(R.drawable.new_edumax_17);
						img_start.setImageResource(R.drawable.new_edumax_16);
					}
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}

			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				
				Log.d(TAG, "PostExecute");
			}
			
			
			
		}.execute("");
	}

	@Override
	public void onClick(View v) {
		
		if(v.equals(img_start))
		{
			//startActivity(new Intent(getActivity(), VideoAct.class));
			if(Global.ispaid)
			{
				((HomeActivity)getActivity()).open_videowindow("edumax");
			}
			else
			{
				((HomeActivity)getActivity()).open_paywindow("edumax");
			}
		}
		else if(v.equals(img_postscript))
		{
			
			Intent webintent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.naver.com/studyviewkr/220358856437"));
			startActivity(webintent);
		}
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		isrunning = false;
		
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
		
		while(lily_edumax.getChildCount() > 0)
		{
			if(lily_edumax.getChildAt(0) instanceof ImageView)
			{
				ImageView imgv = (ImageView)lily_edumax.getChildAt(0);
				
				Drawable d = imgv.getDrawable();
				if(d instanceof BitmapDrawable)
				{
					Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
					bitmap.recycle();
					bitmap = null;
				}
				
				
				d.setCallback(null);
			}
			lily_edumax.removeViewAt(0);
		}
		
		if(lily_edumax_howto != null)
		{
			while(lily_edumax_howto.getChildCount() > 0)
			{
				ImageView imgv = (ImageView)lily_edumax_howto.getChildAt(0);
				
				Drawable d = imgv.getDrawable();
				if(d instanceof BitmapDrawable)
				{
					Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
					bitmap.recycle();
					bitmap = null;
				}
				
				lily_edumax_howto.removeViewAt(0);
				d.setCallback(null);
			}
		}
		
		
		super.onDestroy();
	}
	
	
	
}
